import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { MovieEntity } from './movie.entity';

// 电影和下载链接是一对多的关系 ?
@Entity('movie_download')
export class MovieDownloadEntity {
  @PrimaryGeneratedColumn({ type: 'int' })
  id: number;

  @Column({ type: 'longtext' })
  title: string;

  @Column({ type: 'longtext' })
  text: string;

  @Column({ type: 'longtext' })
  href: string;

  // 添加一个字段, 表示该连接是否还有效
  @Column({ type: 'boolean', default: true })
  isValid: boolean;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  // 多对一关系：多个下载链接属于一个电影
  @ManyToOne(() => MovieEntity, (movie) => movie.downloadUrls, {
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'movieId' }) // 指定外键列名
  movie: MovieEntity;

  @Column({ name: 'movieId', comment: '关联电影的id (外键)', nullable: true })
  movieId: number;
}
